<?php
/**
 * Class that operate on table 'religious_places'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ReligiousPlacesMySqlDAO implements ReligiousPlacesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ReligiousPlacesMySql 
	 */
	public function load($religiousPlace){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_id = "'.$religiousPlace->religiousplaces_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($religiousPlace){
		$sql = 'SELECT * FROM religious_places WHERE '.$religiousPlace->search_by.' like"%'.$religiousPlace->search_by_value.'%" order by '.$religiousPlace->order_by.' '.$religiousPlace->sort_order.' limit '.$religiousPlace->results_per_page.' offset '.$religiousPlace->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function SearchByCount($religiousPlace){
		$sql = 'SELECT count(*) FROM religious_places WHERE '.$religiousPlace->search_by.' like"%'.$religiousPlace->search_by_value.'%" order by '.$religiousPlace->order_by.' '.$religiousPlace->sort_order.' limit '.$religiousPlace->results_per_page.' offset '.$religiousPlace->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll($religiousPlace){
		$sql = 'SELECT * FROM religious_places order by '.$religiousPlace->order_by.' '.$religiousPlace->sort_order.' limit '.$religiousPlace->results_per_page.' offset '.$religiousPlace->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	 public function queryAllCount($religiousPlace){
		$sql = 'SELECT count(*) FROM religious_places order by '.$religiousPlace->order_by.' '.$religiousPlace->sort_order.' limit '.$religiousPlace->results_per_page.' offset '.$religiousPlace->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param religiousPlace primary key
 	 */
	public function delete($religiousPlace){
		$sql = 'UPDATE religious_places SET status = "'.$religiousPlace->status.'" WHERE religiousplaces_id = "'.$religiousPlace->religiousplaces_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ReligiousPlacesMySql religiousPlace
 	 */
	public function insert($religiousPlace){
		$sql = 'INSERT INTO religious_places (religiousplaces_name, religiousplaces_type, religiousplaces_address, religiousplaces_url, religiousplaces_phone1, religiousplaces_phone2, created_date, updated_date) VALUES ("'.$religiousPlace->religiousplaces_name.'", "'.$religiousPlace->religiousplaces_type.'", "'.$religiousPlace->religiousplaces_address.'", "'.$religiousPlace->religiousplaces_url.'", "'.$religiousPlace->religiousplaces_phone1.'", "'.$religiousPlace->religiousplaces_phone2.'", "'.$religiousPlace->created_date.'", "'.$religiousPlace->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$religiousPlace->religiousplaces_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ReligiousPlacesMySql religiousPlace
 	 */
	public function update($religiousPlace){
		$sql = 'UPDATE religious_places SET religiousplaces_name = ?, religiousplaces_type = ?, religiousplaces_address = ?, religiousplaces_url = ?, religiousplaces_phone1 = ?, religiousplaces_phone2 = ?, updated_date = ?, status = ? WHERE religiousplaces_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($religiousPlace->religiousplaces_name);
		$sqlQuery->set($religiousPlace->religiousplaces_type);
		$sqlQuery->set($religiousPlace->religiousplaces_address);
		$sqlQuery->set($religiousPlace->religiousplaces_url);
		$sqlQuery->set($religiousPlace->religiousplaces_phone1);
		$sqlQuery->set($religiousPlace->religiousplaces_phone2);
		$sqlQuery->set($religiousPlace->updated_date);
		$sqlQuery->setNumber($religiousPlace->status);
		$sqlQuery->setNumber($religiousPlace->religiousplaces_id);
		//print_r($sqlQuery);exit;
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM religious_places';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByReligiousplacesName($value){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReligiousplacesType($value){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReligiousplacesAddress($value){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReligiousplacesUrl($value){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReligiousplacesPhone1($value){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_phone1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReligiousplacesPhone2($value){
		$sql = 'SELECT * FROM religious_places WHERE religiousplaces_phone2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM religious_places WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM religious_places WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM religious_places WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByReligiousplacesName($value){
		$sql = 'DELETE FROM religious_places WHERE religiousplaces_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReligiousplacesType($value){
		$sql = 'DELETE FROM religious_places WHERE religiousplaces_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReligiousplacesAddress($value){
		$sql = 'DELETE FROM religious_places WHERE religiousplaces_address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReligiousplacesUrl($value){
		$sql = 'DELETE FROM religious_places WHERE religiousplaces_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReligiousplacesPhone1($value){
		$sql = 'DELETE FROM religious_places WHERE religiousplaces_phone1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReligiousplacesPhone2($value){
		$sql = 'DELETE FROM religious_places WHERE religiousplaces_phone2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM religious_places WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM religious_places WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM religious_places WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ReligiousPlacesMySql 
	 */
	protected function readRow($row){
		$religiousPlace = new ReligiousPlace();
		
		$religiousPlace->religiousplaces_id = $row['religiousplaces_id'];
		$religiousPlace->religiousplaces_name = $row['religiousplaces_name'];
		$religiousPlace->religiousplaces_type = $row['religiousplaces_type'];
		$religiousPlace->religiousplaces_address = $row['religiousplaces_address'];
		$religiousPlace->religiousplaces_url = $row['religiousplaces_url'];
		$religiousPlace->religiousplaces_phone1 = $row['religiousplaces_phone1'];
		$religiousPlace->religiousplaces_phone2 = $row['religiousplaces_phone2'];
		$religiousPlace->created_date = $row['created_date'];
		$religiousPlace->updated_date = $row['updated_date'];
		$religiousPlace->status = $row['status'];

		return $religiousPlace;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ReligiousPlacesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>